package com.andnnl.bytes;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chenss on 2023/12/27.
 */
public class IPValidator {
    // IPv4 地址的正则表达式
    static Pattern pattern = Pattern.compile("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");

    public static void main(String[] args) {
        String ipAddress = "0.168.1.255";
        boolean isValid = isValidIP(ipAddress);
        System.out.println("IP 地址 " + ipAddress + " 是否有效? " + isValid);
    }

    public static boolean isValidIP(String ipAddress) {
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }
}
